package web.servlet;

import domain.Account;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.AccountService;

import java.io.IOException;

//好像完全没用的样子
//好像又有用了
public class AccountServlet extends HttpServlet {
    private static final String ACCOUNT_FORM = "/WEB-INF/jsp/account/AccountForm.jsp";

    private String password;
    private String r_password;
    private String email;
    private String firstName;
    private String lastName;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String phone;
    private String favouriteCategoryId;
    private String languagePreference;
    private boolean listOption;
    private boolean bannerOption;
    private String bannerName;

    private String msg;


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.password = req.getParameter("NewPassword");
        this.r_password = req.getParameter("RepeatPassword");
        this.email = req.getParameter("email");
        this.firstName = req.getParameter("firstName");
        this.lastName = req.getParameter("lastName");
        this.address1 = req.getParameter("address1");
        this.address2 = req.getParameter("address2");
        this.city = req.getParameter("city");
        this.state = req.getParameter("state");
        this.zip = req.getParameter("zip");
        this.country = req.getParameter("country");
        this.languagePreference = req.getParameter("languagePreference");
        this.favouriteCategoryId = req.getParameter("favouriteCategoryId");
        //还有两个变量

        if (isValid()){
            HttpSession session = req.getSession();
            Account account = (Account) session.getAttribute("loginAccount");
            AccountService accountService = new AccountService();
            if (!password.equals("")){
                account.setPassword(password);
            }
            account.setEmail(email);
            account.setFirstName(firstName);
            account.setLastName(lastName);
            account.setAddress1(address1);
            account.setAddress2(address2);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            account.setLanguagePreference(languagePreference);
            account.setFavouriteCategoryId(favouriteCategoryId);
            accountService.updateAccount(account);
            session.setAttribute("loginAccount",account);
            resp.sendRedirect("main");
        }
       else {
           req.setAttribute("updateMsg",this.msg);
           req.getRequestDispatcher(ACCOUNT_FORM).forward(req,resp);
        }
    }

    Boolean isValid(){
        if (!password.equals("") && !r_password.equals("")){
            if (password.equals(r_password)){
                return true;
            }
            else {
                this.msg = "请输入相同的密码";
                return false;
            }
        }else {
            if (password.equals("")&&r_password.equals("")){
                return true;
            }else {
                this.msg = "请确认密码两个均已填写！";
                return false;
            }
        }
    }
}
